﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace CLUBsInterpreter.ObjectTypes
{
	/**
	 * <summary>
	 * Describes a piece object in the CLUBs environment.
	 * </summary>
	 **/
	class CLUBsPiece : CLUBsObject, Visual.IPaintable
	{

		public static Dictionary<string,Image> Icons = new Dictionary<string,Image>();

		private static int s_nextPrototypeID = 0;

		public int PrototypeID = -1;

		/**
		 * <summary>
		 * Default constructor creates piece, setting default properties.
		 * </summary>
		 **/
		public CLUBsPiece(bool newProto = false)
		{
			if (newProto) PrototypeID = s_nextPrototypeID++;
			// Set the object identifier type
			ObjectIdentifierType = AST.IdentifierType.Piece;
			// SPECIFY STANDARD ENVIRONMENT
			// Icon variable specifying the path of the icon to use for this piece
			LocalVariables.Declare("Icon", new CLUBsString());
		}

		/**
		 * <see cref="CLUBsObject.CopyValueFromObject"/>
		 **/
        
		public override void CopyValueFromObject(CLUBsObject obj)
		{
            CLUBsPiece pieceObj = (CLUBsPiece) obj;
			PrototypeID = pieceObj.PrototypeID;
            LocalVariables.Clear();

            foreach (KeyValuePair<string, CLUBsObject> var in obj.LocalVariables)
            {
				LocalVariables.Add(var.Key, var.Value);
				LocalVariables[var.Key].CopyValueFromObject(var.Value);
            }
		}

		public void Paint(object sender, System.Windows.Forms.PaintEventArgs args)
		{

			if (!Icons.ContainsKey((CLUBsString)LocalVariables["Icon"]))
			{
				Icons.Add((CLUBsString)LocalVariables["Icon"], Image.FromFile((CLUBsString)LocalVariables["Icon"]));
			}

			Graphics g = args.Graphics;
			Rectangle clip = args.ClipRectangle;
			Rectangle cellRect = new Rectangle(clip.X, clip.Y, clip.Width - 1, clip.Height - 1);

			g.DrawImage(Icons[(CLUBsString)LocalVariables["Icon"]], cellRect);
		}
	}
}
